/*     */ package com.zimbra.cs.ldap.unboundid;
/*     */ 
/*     */ import com.google.common.collect.Lists;
/*     */ import com.google.common.collect.Maps;
/*     */ import com.unboundid.ldap.listener.InMemoryDirectoryServer;
/*     */ import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
/*     */ import com.unboundid.ldap.sdk.LDAPConnection;
/*     */ import com.unboundid.ldap.sdk.LDAPConnectionPool;
/*     */ import com.unboundid.ldap.sdk.LDAPException;
/*     */ import com.unboundid.ldap.sdk.SearchResult;
/*     */ import com.unboundid.ldap.sdk.SearchResultEntry;
/*     */ import com.unboundid.ldap.sdk.SearchScope;
/*     */ import com.unboundid.ldap.sdk.schema.Schema;
/*     */ import com.unboundid.ldif.LDIFException;
/*     */ import com.unboundid.ldif.LDIFWriter;
/*     */ import com.zimbra.common.localconfig.DebugConfig;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.auth.PasswordUtil.SSHA;
/*     */ import com.zimbra.cs.ldap.LdapClient;
/*     */ import com.zimbra.cs.ldap.LdapException;
/*     */ import com.zimbra.cs.ldap.LdapServerConfig;
/*     */ import com.zimbra.cs.ldap.LdapServerConfig.ExternalLdapConfig;
/*     */ import com.zimbra.cs.ldap.LdapUsage;
/*     */ import com.zimbra.cs.ldap.LdapUtil;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class InMemoryLdapServer
/*     */ {
/*     */   public static final String ZIMBRA_LDAP_SERVER = "ZIMBRA_LDAP_SERVER";
/*     */   public static final String UNITTEST_BASE_DOMAIN_SEGMENT = "unittest";
/*  53 */   private static final String UNITTEST_DATA_PATH = LC.zimbra_home.value() + "/unittest/ldap/InMemoryLdapServer";
/*     */   
/*     */   private static final String SCHEMA_FILE_NAME = "zimbra_schema.ldif";
/*     */   
/*     */   private static final String DIT_FILE_NAME = "zimbra_dit.ldif";
/*     */   
/*  59 */   private static Map<String, Server> servers = Maps.newHashMap();
/*     */   
/*     */   public static boolean isOn() {
/*  62 */     return DebugConfig.useInMemoryLdapServer;
/*     */   }
/*     */   
/*     */   private static String getSchemaFileName(String path) {
/*  66 */     return path + "/" + "zimbra_schema.ldif";
/*     */   }
/*     */   
/*     */   private static String getDITFileName(String path) {
/*  70 */     return path + "/" + "zimbra_dit.ldif";
/*     */   }
/*     */   
/*     */   private static synchronized Server getServer(String serverName) {
/*  74 */     return (Server)servers.get(serverName);
/*     */   }
/*     */   
/*     */   private static synchronized void addServer(String serverName, Server server) {
/*  78 */     servers.put(serverName, server);
/*     */   }
/*     */   
/*     */   private static synchronized void removeServer(String serverName) {
/*  82 */     servers.remove(serverName);
/*     */   }
/*     */   
/*     */   public static synchronized void start(String serverName, ServerConfig serverConfig) throws LdapException
/*     */   {
/*  87 */     Server server = getServer(serverName);
/*  88 */     if (server != null) {
/*  89 */       throw LdapException.LDAP_ERROR("in memory LDAP server is already started", null);
/*     */     }
/*     */     
/*  92 */     server = new Server(serverConfig, null);
/*  93 */     server.start();
/*  94 */     addServer(serverName, server);
/*     */   }
/*     */   
/*     */   public static synchronized void stop(String serverName) throws LdapException {
/*  98 */     Server server = getServer(serverName);
/*  99 */     if (server == null) {
/* 100 */       throw LdapException.LDAP_ERROR("in memory LDAP server was not started", null);
/*     */     }
/*     */     
/* 103 */     server.stop();
/* 104 */     removeServer(serverName);
/*     */   }
/*     */   
/*     */   static synchronized LDAPConnectionPool createConnPool(String serverName, LdapServerConfig config) throws LdapException
/*     */   {
/* 109 */     Server server = getServer(serverName);
/* 110 */     if (server == null) {
/* 111 */       throw LdapException.LDAP_ERROR("in memory LDAP server was not started", null);
/*     */     }
/*     */     
/* 114 */     return server.getConnectionPool(config);
/*     */   }
/*     */   
/*     */   static LDAPConnection getConnection()
/*     */     throws LdapException
/*     */   {
/* 120 */     Server server = getServer("ZIMBRA_LDAP_SERVER");
/* 121 */     return server.getConnection();
/*     */   }
/*     */   
/*     */   static Schema getSchema(String serverName) throws LdapException {
/* 125 */     Server server = getServer(serverName);
/* 126 */     if (server == null) {
/* 127 */       throw LdapException.LDAP_ERROR("in memory LDAP server was not started", null);
/*     */     }
/*     */     
/* 130 */     return server.getSchema();
/*     */   }
/*     */   
/*     */   public static void export() throws LdapException {
/* 134 */     String path = LC.zimbra_tmp_directory.value() + "/inmem_ldap_%s.ldif";
/*     */     
/* 136 */     for (Map.Entry<String, Server> entry : servers.entrySet()) {
/* 137 */       String serverName = (String)entry.getKey();
/* 138 */       Server server = (Server)entry.getValue();
/* 139 */       String filePath = String.format(path, new Object[] { serverName });
/* 140 */       server.export(filePath);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static class Password
/*     */   {
/* 156 */     private static String NON_SSHA_PASSWORD_PREFIX = "__non_ssha__";
/*     */     
/*     */     public static String genNonSSHAPassword(String password) {
/* 159 */       return NON_SSHA_PASSWORD_PREFIX + password;
/*     */     }
/*     */     
/*     */     public static String treatPassword(String password) {
/* 163 */       if (password.startsWith(NON_SSHA_PASSWORD_PREFIX)) {
/* 164 */         return password;
/*     */       }
/* 166 */       return PasswordUtil.SSHA.generateSSHA(password, null);
/*     */     }
/*     */   }
/*     */   
/*     */   public static class ServerConfig
/*     */   {
/*     */     private String schemaLDIFFile;
/*     */     private String ditLDIFFile;
/*     */     private List<String> extraBaseDNs;
/*     */     
/*     */     public ServerConfig() {
/* 177 */       this(InMemoryLdapServer.UNITTEST_DATA_PATH, null);
/*     */     }
/*     */     
/*     */     public ServerConfig(List<String> extraBaseDNs) {
/* 181 */       this(InMemoryLdapServer.UNITTEST_DATA_PATH, extraBaseDNs);
/*     */     }
/*     */     
/*     */     public ServerConfig(String path, List<String> extraBaseDNs) {
/* 185 */       this(InMemoryLdapServer.getSchemaFileName(path), InMemoryLdapServer.getDITFileName(path), extraBaseDNs);
/*     */     }
/*     */     
/*     */     public ServerConfig(String schemaLDIFFile, String ditLDIFFile, List<String> extraBaseDNs)
/*     */     {
/* 190 */       this.schemaLDIFFile = schemaLDIFFile;
/* 191 */       this.ditLDIFFile = ditLDIFFile;
/* 192 */       this.extraBaseDNs = extraBaseDNs;
/*     */     }
/*     */     
/*     */     private String schemaLDIFFile() {
/* 196 */       return this.schemaLDIFFile;
/*     */     }
/*     */     
/*     */     private String ditLDIFFile() {
/* 200 */       return this.ditLDIFFile;
/*     */     }
/*     */     
/*     */     private List<String> extraBaseDNs() {
/* 204 */       return this.extraBaseDNs;
/*     */     }
/*     */   }
/*     */   
/*     */   private static class Server
/*     */   {
/*     */     private InMemoryLdapServer.ServerConfig serverConfig;
/*     */     private InMemoryDirectoryServer server;
/*     */     
/*     */     private Server(InMemoryLdapServer.ServerConfig serverConfig)
/*     */     {
/* 215 */       this.serverConfig = serverConfig;
/*     */     }
/*     */     
/*     */     private boolean started() {
/* 219 */       return this.server != null;
/*     */     }
/*     */     
/*     */     private void start() throws LdapException {
/* 223 */       if (started()) {
/* 224 */         throw LdapException.LDAP_ERROR("in memory LDAP server is already started", null);
/*     */       }
/*     */       
/* 227 */       boolean started = false;
/*     */       try {
/* 229 */         Schema schema = Schema.getSchema(new String[] { InMemoryLdapServer.ServerConfig.access$1000(this.serverConfig) });
/*     */         
/*     */ 
/*     */ 
/* 233 */         String domainName = LC.zimbra_server_hostname.value();
/* 234 */         String topLevelDomainDN = LdapUtil.domainToTopLevelDN(domainName);
/*     */         
/* 236 */         List<String> baseDNs = Lists.newArrayList(new String[] { "cn=zimbra", topLevelDomainDN, "dc=com" });
/* 237 */         List<String> extraBaseDNs = InMemoryLdapServer.ServerConfig.access$1100(this.serverConfig);
/* 238 */         if (extraBaseDNs != null) {
/* 239 */           baseDNs.addAll(extraBaseDNs);
/*     */         }
/*     */         
/*     */ 
/* 243 */         InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig((String[])baseDNs.toArray(new String[baseDNs.size()]));
/*     */         
/*     */ 
/* 246 */         config.addAdditionalBindCredentials("cn=config", LC.ldap_root_password.value());
/* 247 */         config.setSchema(schema);
/* 248 */         config.setGenerateOperationalAttributes(true);
/*     */         
/* 250 */         this.server = new InMemoryDirectoryServer(config);
/* 251 */         this.server.importFromLDIF(true, InMemoryLdapServer.ServerConfig.access$1200(this.serverConfig));
/* 252 */         this.server.startListening();
/* 253 */         started = true;
/*     */       }
/*     */       catch (LDIFException e) {
/* 256 */         throw LdapException.LDAP_ERROR("failed to start in memory ldap server", e);
/*     */       } catch (LDAPException e) {
/* 258 */         throw LdapException.LDAP_ERROR("failed to start in memory ldap server", e);
/*     */       } catch (IOException e) {
/* 260 */         throw LdapException.LDAP_ERROR("failed to start in memory ldap server", e);
/*     */       } finally {
/* 262 */         if (!started) {
/* 263 */           this.server = null;
/*     */         }
/*     */       }
/*     */     }
/*     */     
/*     */     private void stop() throws LdapException {
/* 269 */       if (!started()) {
/* 270 */         throw LdapException.LDAP_ERROR("in memory LDAP server was not started", null);
/*     */       }
/*     */       
/* 273 */       this.server.shutDown(true);
/*     */     }
/*     */     
/*     */     private Schema getSchema() throws LdapException {
/*     */       try {
/* 278 */         return this.server.getSchema();
/*     */       } catch (LDAPException e) {
/* 280 */         throw UBIDLdapException.mapToLdapException(e);
/*     */       }
/*     */     }
/*     */     
/*     */     private void export(String path) throws LdapException {
/*     */       try {
/* 286 */         boolean excludeGeneratedAttrs = false;
/* 287 */         boolean excludeChangeLog = true;
/* 288 */         this.server.exportToLDIF(path, excludeGeneratedAttrs, excludeChangeLog);
/*     */       } catch (LDAPException e) {
/* 290 */         throw UBIDLdapException.mapToLdapException(e);
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     private LDAPConnectionPool getConnectionPool(LdapServerConfig config)
/*     */       throws LdapException
/*     */     {
/*     */       try
/*     */       {
/* 306 */         int initPoolSize = config.getConnPoolInitSize();
/* 307 */         if (((config instanceof LdapServerConfig.ExternalLdapConfig)) && 
/* 308 */           (initPoolSize < 1)) {
/* 309 */           initPoolSize = 1;
/*     */         }
/*     */         
/* 312 */         assert (initPoolSize >= 1);
/*     */         
/*     */ 
/* 315 */         return this.server.getConnectionPool(null, null, initPoolSize, config.getConnPoolMaxSize());
/*     */       }
/*     */       catch (LDAPException e) {
/* 318 */         throw UBIDLdapException.mapToLdapException(e);
/*     */       }
/*     */     }
/*     */     
/*     */     private LDAPConnection getConnection() throws LdapException {
/*     */       try {
/* 324 */         return this.server.getConnection();
/*     */       } catch (LDAPException e) {
/* 326 */         throw UBIDLdapException.mapToLdapException(e);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private static class Exporter
/*     */   {
/*     */     private PrintStream logger;
/*     */     
/*     */     private String path;
/*     */     
/*     */ 
/*     */     private Exporter(String path)
/*     */     {
/* 342 */       this(path, System.out);
/*     */     }
/*     */     
/*     */     private Exporter(String path, PrintStream logger) {
/* 346 */       this.logger = logger;
/* 347 */       this.path = path;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     private void generateLDIF()
/*     */     {
/* 356 */       UBIDLdapContext zlc = null;
/*     */       try
/*     */       {
/* 359 */         zlc = (UBIDLdapContext)LdapClient.getContext(LdapUsage.UNITTEST);
/* 360 */         LDAPConnection conn = zlc.getNative();
/*     */         
/*     */ 
/*     */ 
/*     */ 
/* 365 */         String filePath = InMemoryLdapServer.getSchemaFileName(this.path);
/* 366 */         this.logger.println("============================================");
/* 367 */         this.logger.println("    Generating " + filePath);
/* 368 */         this.logger.println("============================================");
/* 369 */         Schema schema = conn.getSchema();
/* 370 */         LDIFWriter schemaWriter = new LDIFWriter(filePath);
/* 371 */         schemaWriter.writeEntry(schema.getSchemaEntry());
/* 372 */         schemaWriter.close();
/*     */         
/* 374 */         this.logger.println();
/*     */         
/*     */ 
/*     */ 
/*     */ 
/* 379 */         filePath = InMemoryLdapServer.getDITFileName(this.path);
/* 380 */         this.logger.println("============================================");
/* 381 */         this.logger.println("    Generating " + filePath);
/* 382 */         this.logger.println("============================================");
/* 383 */         LDIFWriter ditWriter = new LDIFWriter(filePath);
/* 384 */         SearchResult searchResult = conn.search("", SearchScope.SUB, "(objectClass=*)", new String[0]);
/*     */         
/* 386 */         for (SearchResultEntry entry : searchResult.getSearchEntries()) {
/* 387 */           ditWriter.writeEntry(entry);
/*     */         }
/* 389 */         ditWriter.close();
/*     */       }
/*     */       catch (ServiceException e) {
/* 392 */         this.logger.println("Failed to generate LDIF files for InMemoryLdapSerer");
/* 393 */         e.printStackTrace(this.logger);
/*     */       } catch (LDAPException e) {
/* 395 */         this.logger.println("Failed to generate LDIF files for InMemoryLdapSerer");
/* 396 */         e.printStackTrace(this.logger);
/*     */       } catch (IOException e) {
/* 398 */         this.logger.println("Failed to generate LDIF files for InMemoryLdapSerer");
/* 399 */         e.printStackTrace(this.logger);
/*     */       } finally {
/* 401 */         LdapClient.closeContext(zlc);
/*     */       }
/*     */     }
/*     */     
/*     */     private Schema getSchema() throws ServiceException {
/* 406 */       UBIDLdapContext zlc = null;
/*     */       try
/*     */       {
/* 409 */         zlc = (UBIDLdapContext)LdapClient.getContext(LdapUsage.UNITTEST);
/* 410 */         LDAPConnection conn = zlc.getNative();
/* 411 */         Schema schema = conn.getSchema();
/* 412 */         return schema;
/*     */       } catch (LDAPException e) {
/* 414 */         throw ServiceException.FAILURE("unable to extract schema", e);
/*     */       } finally {
/* 416 */         LdapClient.closeContext(zlc);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception {
/* 422 */     String path = args[0];
/*     */     
/* 424 */     Exporter exporter = new Exporter(path, null);
/* 425 */     exporter.generateLDIF();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/ldap/unboundid/InMemoryLdapServer.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */